/**
 * 文件模型
 *
 * Created by snowingsea on 2020/02/06.
 */
const mongoose = require('mongoose');
const schemaDefinition = require('./schema/user-defined-fields');
const schemaInstance = mongoose.Schema(schemaDefinition);
schemaInstance.index({ scope: 1, name: 1, domain: 1 }, { unique: true });
schemaInstance.set('autoIndex', false);
const modelInstance = mongoose.model('user-defined-fields', schemaInstance);
mongoose.connection.on('open', () => modelInstance.syncIndexes().catch(() => { }));

if (!schemaInstance.options.toObject) schemaInstance.options.toObject = {};
schemaInstance.options.toObject.transform = function transform(doc, ret) {
  // remove the _id of every document before returning the result
  ret.key = ret._id.toHexString();
  delete ret._id;
  delete ret.__v;
  return ret;
};
/**
 * Export model definition object.
 */
module.exports = modelInstance;
